package com.todoit.wavebot.util;
import com.google.appengine.api.datastore.Key;

import java.util.Date;
import java.util.Calendar;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import java.util.Random;

@PersistenceCapable
public class TodoitTask implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// probably got to have one of those for DB... meh
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

	// FIXME: use proper get/set...
	@Persistent
	public String id;
	
	@Persistent
	public String title;
	
	@Persistent
	Integer priority;
	
	@Persistent
	Date dueDate;
	
	@Persistent
	String location;
	
	@Persistent
	String tags; // perhaps some smarter storage?
	
	@Persistent
	String link;
	
	@Persistent
	Integer progress; // = -1 to represent deleted task
	
	@Persistent
	String status;

	@Persistent
	String creator;
	
	// FIXME: get/set
	@Persistent
	public String owner;
	
	@Persistent
	String lastModifier;
	
	@Persistent
	Date lastModified;

	@Persistent
	Date completedDate; // if null - not completed
	
	@Persistent
	String completer;
	
	@Persistent
	Date addedDate;
	
	@Persistent
	String freq; // = 'after 1 day', 'daily', ...
	
	@Persistent
	String dur;
	
	public String toCSV() {
		return "\"" + id + "\"," +
		"\"" + title + "\"," +
		"\"" + priority + "\"," + 
		"\"" + dueDate + "\"," +
		"\"" + location + "\"," +
		"\"" + tags + "\"," +
		"\"" + link + "\"," +
		"\"" + progress + "\"," + 
		"\"" + status + "\"," +
		"\"" + creator + "\"," +
		"\"" + owner + "\"," +
		"\"" + lastModifier + "\"," +
		"\"" + lastModified + "\"," +
		"\"" + completedDate + "\"," +
		"\"" + completer + "\"," +
		"\"" + addedDate + "\"," +
		"\"" + freq + "\"," +
		"\"" + dur + "\"";
	}
	
	/* this is useless here, because we purge them from the list when we ser/des
	this.isDeleted = function() {
		return this.progress == -1;
	};
	*/
	
	// even if we don't use this method, it makes sure the key field isn't redundant
	public Key getKey() {
        return key;
    }

	public Boolean isCompleted() {
		return this.completedDate != null;
	};
	
	
	public Boolean isToday() {
		Calendar cal = Calendar.getInstance(); // right format?
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return (dueDate.toString() == sdf.format(cal.getTime()));
	};
	
	public Boolean isOverdue() {
		// not completed and late? (not including Today)
		//return (!this.isCompleted() && this.dueDate != null && !this.isToday() 
		//	&& this.dueDate.getTime() < (new Date()).getTime());
		// until i fix the isOverDue, hardcoded to true for demonstration purposes
		return true;
	};
	
	public TodoitTask(int i) {	// real tasks will be constructed from CSV or from gadget
		if (1==i)
		{
			id = "ID 1";
			title = "Title 1";
			priority = 1;
			dueDate = new Date("1/1/2008");
			location = "hell";
			tags = "foo bar gzonk"; 
			link = "";
			progress = 0;
			status = "";

			creator = "";
			owner = "coutal@gmail.com";
			lastModifier = "Enma Ai";
			lastModified = new Date("1/1/2008");

			completedDate = null; 
			completer = null;
			
			addedDate = null;
			freq = ""; 
			dur = "";

		}
		if (2==i)
		{
			id = "ID 2";
			title = "Title 2";
			priority = 1;
			dueDate = new Date("1/1/2009");
			location = "pandemonium";
			tags = "foo bar gzonk"; 
			link = "";
			progress = 0;
			status = "";

			creator = "";
			owner = "coutal@gmail.com";
			lastModifier = "Enma Ai";
			lastModified = new Date("1/1/2009");

			completedDate = null; 
			completer = null;
			
			addedDate = null;
			freq = ""; 
			dur = "";

		}

	}

	public TodoitTask(String taskText, String User) {
		Random generator = new Random();
		id = "Task_" + generator.nextInt();
		title = taskText;
		priority = 0;
		dueDate = new Date("1/1/2009");
		location = null;
		tags = null; 
		link = null;
		progress = 0;
		status = null;

		creator = null;
		owner = User;
		lastModifier = User;
		lastModified = new Date("1/1/2009");

		completedDate = null; 
		completer = null;
		
		addedDate = null;
		freq = null; 
		dur = null;

	}
}
